package com.balabala.gym_goods.service.impl;

import com.balabala.api.UserFeignClient;
import com.balabala.dto.UserNameDto;
import com.balabala.gym_goods.mapper.EvaluationMapper;
import com.balabala.gym_goods.pojo.dto.QueryTEDto;
import com.balabala.gym_goods.pojo.entity.Evaluation;
import com.balabala.gym_goods.pojo.vo.EvaluationVo;
import com.balabala.gym_goods.service.EvaluationService;
import com.balabala.gym_system.api.CoachFeignClient;
import com.balabala.gym_system.pojo.vo.CoachVo;
import com.balabala.result.Result;
import com.balabala.vo.UserNameVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fsj
 * @date 2023/5/5 18:19
 */
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {

    //远程调用user服务的接口
    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Autowired
    private CoachFeignClient coachFeignClient;

    //根据商品id查询评价
    @Override
    public List<Evaluation> findEvaluationBySpuId(Long spuId) {
        return getBaseMapper().findEvaluationBySpuId(spuId);
    }

    //根据团课id查评价
    @Override
    public List<Evaluation> findEvaluationByCourseId(Long courseId) {
        return  getBaseMapper().findEvaluationByCourseId(courseId);

    }
    //根据私教课id查评价
    @Override
    public List<Evaluation> findEvaluationByCoachSchId(Long coachId) {
        return  getBaseMapper().findEvaluationByCoachId(coachId);

    }

    @Override
    public List<EvaluationVo> findVipIdBy(UserNameDto userNameDto) {

        //远程调用获得会员名称和id
        List<UserNameVo> data = userFeignClient.findVipIdBy(userNameDto).getData();

        List<EvaluationVo> evaluationVo=new ArrayList<>();

        data.forEach(d->{

            //new 一个条件
            QueryTEDto queryTEDto=new QueryTEDto();
            queryTEDto.setVipId(d.getId());
            queryTEDto.setCourseName(userNameDto.getCourseName());
            queryTEDto.setPage(userNameDto.getPage());
            queryTEDto.setLimit(userNameDto.getLimit());
            //根据会员id 条件去查询评价
           List<EvaluationVo>  evaluations = evaluationMapper.findVipIdBy(queryTEDto);

            evaluations.forEach(e->{
                //设置会员名称
                e.setVipName(d.getName());

                Long coachId = Long.valueOf(e.getCoachId());

                //远程调用获取教练名称
                Result<CoachVo> coachById = coachFeignClient.getCoachById(coachId);

                //设置教练名称
                e.setCoachName(coachById.getData().getName());

                //拼接评价时间

            });

            evaluations.forEach(e1->{
                EvaluationVo evaluationVo1=new EvaluationVo();

                BeanUtils.copyProperties(e1,evaluationVo1);
                evaluationVo.add(evaluationVo1);
            });
        });

        return evaluationVo;
    }
}
